﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Address;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Melsec
{
	/// <summary>
	/// <b>[商业授权]</b> 三菱MC协议的虚拟服务器，支持M,X,Y,D,W的数据池读写操作，支持二进制及ASCII格式进行读写操作，需要在实例化的时候指定。<br />
	/// <b>[Authorization]</b> The Mitsubishi MC protocol virtual server supports M, X, Y, D, W data pool read and write operations, 
	/// and supports binary and ASCII format read and write operations, which need to be specified during instantiation.
	/// </summary>
	/// <remarks>
	/// 本三菱的虚拟PLC仅限商业授权用户使用，感谢支持。
	/// 如果你没有可以测试的三菱PLC，想要测试自己开发的上位机软件，或是想要在本机实现虚拟PLC，然后进行IO的输入输出练习，都可以使用本类来实现，先来说明下地址信息
	/// <br />
	/// 地址的输入的格式说明如下：
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>示例</term>
	///     <term>地址进制</term>
	///     <term>字操作</term>
	///     <term>位操作</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>内部继电器</term>
	///     <term>M</term>
	///     <term>M100,M200</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输入继电器</term>
	///     <term>X</term>
	///     <term>X100,X1A0</term>
	///     <term>16</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输出继电器</term>
	///     <term>Y</term>
	///     <term>Y100,Y1A0</term>
	///     <term>16</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>数据寄存器</term>
	///     <term>D</term>
	///     <term>D1000,D2000</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>×</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>链接寄存器</term>
	///     <term>W</term>
	///     <term>W100,W1A0</term>
	///     <term>16</term>
	///     <term>√</term>
	///     <term>×</term>
	///     <term></term>
	///   </item>
	/// </list>
	/// </remarks>
	// Token: 0x02000074 RID: 116
	public class MelsecMcServer : NetworkDataServerBase
	{
		/// <summary>
		/// 实例化一个默认参数的mc协议的服务器<br />
		/// Instantiate a mc protocol server with default parameters
		/// </summary>
		/// <param name="isBinary">是否是二进制，默认是二进制，否则是ASCII格式</param>
		// Token: 0x06000992 RID: 2450 RVA: 0x00037B7C File Offset: 0x00035D7C
		public MelsecMcServer(bool isBinary = true)
		{
			this.xBuffer = new SoftBuffer(65536);
			this.yBuffer = new SoftBuffer(65536);
			this.mBuffer = new SoftBuffer(65536);
			this.dBuffer = new SoftBuffer(131072);
			this.wBuffer = new SoftBuffer(131072);
			base.WordLength = 1;
			base.ByteTransform = new RegularByteTransform();
			this.isBinary = isBinary;
		}

		/// <inheritdoc />
		// Token: 0x06000993 RID: 2451 RVA: 0x00037C04 File Offset: 0x00035E04
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.M.DataCode;
				if (flag2)
				{
					bool[] array = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.mBuffer.GetBytes(operateResult.Content.AddressStart, (int)(length * 16)), (byte m) => m > 0));
					result = OperateResult.CreateSuccessResult<byte[]>(SoftBasic.BoolArrayToByte(array));
				}
				else
				{
					bool flag3 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.X.DataCode;
					if (flag3)
					{
						bool[] array2 = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.xBuffer.GetBytes(operateResult.Content.AddressStart, (int)(length * 16)), (byte m) => m > 0));
						result = OperateResult.CreateSuccessResult<byte[]>(SoftBasic.BoolArrayToByte(array2));
					}
					else
					{
						bool flag4 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.Y.DataCode;
						if (flag4)
						{
							bool[] array3 = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.yBuffer.GetBytes(operateResult.Content.AddressStart, (int)(length * 16)), (byte m) => m > 0));
							result = OperateResult.CreateSuccessResult<byte[]>(SoftBasic.BoolArrayToByte(array3));
						}
						else
						{
							bool flag5 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.D.DataCode;
							if (flag5)
							{
								result = OperateResult.CreateSuccessResult<byte[]>(this.dBuffer.GetBytes(operateResult.Content.AddressStart * 2, (int)(length * 2)));
							}
							else
							{
								bool flag6 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.W.DataCode;
								if (flag6)
								{
									result = OperateResult.CreateSuccessResult<byte[]>(this.wBuffer.GetBytes(operateResult.Content.AddressStart * 2, (int)(length * 2)));
								}
								else
								{
									result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000994 RID: 2452 RVA: 0x00037E40 File Offset: 0x00036040
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address, 0);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.M.DataCode;
				if (flag2)
				{
					byte[] data = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(value), (bool m) => m ? 1 : 0));
					this.mBuffer.SetBytes(data, operateResult.Content.AddressStart);
					result = OperateResult.CreateSuccessResult();
				}
				else
				{
					bool flag3 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.X.DataCode;
					if (flag3)
					{
						byte[] data2 = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(value), (bool m) => m ? 1 : 0));
						this.xBuffer.SetBytes(data2, operateResult.Content.AddressStart);
						result = OperateResult.CreateSuccessResult();
					}
					else
					{
						bool flag4 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.Y.DataCode;
						if (flag4)
						{
							byte[] data3 = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(value), (bool m) => m ? 1 : 0));
							this.yBuffer.SetBytes(data3, operateResult.Content.AddressStart);
							result = OperateResult.CreateSuccessResult();
						}
						else
						{
							bool flag5 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.D.DataCode;
							if (flag5)
							{
								this.dBuffer.SetBytes(value, operateResult.Content.AddressStart * 2);
								result = OperateResult.CreateSuccessResult();
							}
							else
							{
								bool flag6 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.W.DataCode;
								if (flag6)
								{
									this.wBuffer.SetBytes(value, operateResult.Content.AddressStart * 2);
									result = OperateResult.CreateSuccessResult();
								}
								else
								{
									result = new OperateResult<byte[]>(StringResources.Language.NotSupportedDataType);
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000995 RID: 2453 RVA: 0x00038074 File Offset: 0x00036274
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address, 0);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.McDataType.DataType == 0;
				if (flag2)
				{
					result = new OperateResult<bool[]>(StringResources.Language.MelsecCurrentTypeNotSupportedWordOperate);
				}
				else
				{
					bool flag3 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.M.DataCode;
					if (flag3)
					{
						result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.mBuffer.GetBytes(operateResult.Content.AddressStart, (int)length), (byte m) => m > 0)));
					}
					else
					{
						bool flag4 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.X.DataCode;
						if (flag4)
						{
							result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.xBuffer.GetBytes(operateResult.Content.AddressStart, (int)length), (byte m) => m > 0)));
						}
						else
						{
							bool flag5 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.Y.DataCode;
							if (flag5)
							{
								result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.yBuffer.GetBytes(operateResult.Content.AddressStart, (int)length), (byte m) => m > 0)));
							}
							else
							{
								result = new OperateResult<bool[]>(StringResources.Language.NotSupportedDataType);
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000996 RID: 2454 RVA: 0x00038224 File Offset: 0x00036424
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] value)
		{
			OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address, 0);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content.McDataType.DataType == 0;
				if (flag2)
				{
					result = new OperateResult<bool[]>(StringResources.Language.MelsecCurrentTypeNotSupportedWordOperate);
				}
				else
				{
					bool flag3 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.M.DataCode;
					if (flag3)
					{
						this.mBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(value, (bool m) => m ? 1 : 0)), operateResult.Content.AddressStart);
						result = OperateResult.CreateSuccessResult();
					}
					else
					{
						bool flag4 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.X.DataCode;
						if (flag4)
						{
							this.xBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(value, (bool m) => m ? 1 : 0)), operateResult.Content.AddressStart);
							result = OperateResult.CreateSuccessResult();
						}
						else
						{
							bool flag5 = operateResult.Content.McDataType.DataCode == MelsecMcDataType.Y.DataCode;
							if (flag5)
							{
								this.yBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(value, (bool m) => m ? 1 : 0)), operateResult.Content.AddressStart);
								result = OperateResult.CreateSuccessResult();
							}
							else
							{
								result = new OperateResult<bool[]>(StringResources.Language.NotSupportedDataType);
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000997 RID: 2455 RVA: 0x000383DC File Offset: 0x000365DC
		protected override void ThreadPoolLoginAfterClientCheck(Socket socket, IPEndPoint endPoint)
		{
			AppSession appSession = new AppSession();
			appSession.IpEndPoint = endPoint;
			appSession.WorkSocket = socket;
			try
			{
				socket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(this.SocketAsyncCallBack), appSession);
				base.AddClient(appSession);
			}
			catch
			{
				socket.Close();
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteDebug(this.ToString(), string.Format(StringResources.Language.ClientOfflineInfo, endPoint));
				}
			}
		}

		// Token: 0x06000998 RID: 2456 RVA: 0x00038470 File Offset: 0x00036670
		[DebuggerStepThrough]
		private void SocketAsyncCallBack(IAsyncResult ar)
		{
			MelsecMcServer.<SocketAsyncCallBack>d__6 <SocketAsyncCallBack>d__ = new MelsecMcServer.<SocketAsyncCallBack>d__6();
			<SocketAsyncCallBack>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<SocketAsyncCallBack>d__.<>4__this = this;
			<SocketAsyncCallBack>d__.ar = ar;
			<SocketAsyncCallBack>d__.<>1__state = -1;
			<SocketAsyncCallBack>d__.<>t__builder.Start<MelsecMcServer.<SocketAsyncCallBack>d__6>(ref <SocketAsyncCallBack>d__);
		}

		/// <summary>
		/// 当收到mc协议的报文的时候应该触发的方法，允许继承重写，来实现自定义的返回，或是数据监听。<br />
		/// The method that should be triggered when a message of the mc protocol is received, 
		/// allowing inheritance to be rewritten to implement custom return or data monitoring.
		/// </summary>
		/// <param name="mcCore">mc报文</param>
		/// <returns>返回的报文信息</returns>
		// Token: 0x06000999 RID: 2457 RVA: 0x000384B0 File Offset: 0x000366B0
		protected virtual byte[] ReadFromMcCore(byte[] mcCore)
		{
			bool flag = mcCore[0] == 1 && mcCore[1] == 4;
			byte[] result;
			if (flag)
			{
				result = this.PackCommand(this.ReadByCommand(mcCore));
			}
			else
			{
				bool flag2 = mcCore[0] == 1 && mcCore[1] == 20;
				if (flag2)
				{
					result = this.PackCommand(this.WriteByMessage(mcCore));
				}
				else
				{
					result = null;
				}
			}
			return result;
		}

		/// <summary>
		/// 当收到mc协议的报文的时候应该触发的方法，允许继承重写，来实现自定义的返回，或是数据监听。<br />
		/// The method that should be triggered when a message of the mc protocol is received, 
		/// allowing inheritance to be rewritten to implement custom return or data monitoring.
		/// </summary>
		/// <param name="mcCore">mc报文</param>
		/// <returns>返回的报文信息</returns>
		// Token: 0x0600099A RID: 2458 RVA: 0x0003850C File Offset: 0x0003670C
		protected virtual byte[] ReadFromMcAsciiCore(byte[] mcCore)
		{
			bool flag = mcCore[0] == 48 && mcCore[1] == 52 && mcCore[2] == 48 && mcCore[3] == 49;
			byte[] result;
			if (flag)
			{
				result = this.PackCommand(this.ReadAsciiByCommand(mcCore));
			}
			else
			{
				bool flag2 = mcCore[0] == 49 && mcCore[1] == 52 && mcCore[2] == 48 && mcCore[3] == 49;
				if (flag2)
				{
					result = this.PackCommand(this.WriteAsciiByMessage(mcCore));
				}
				else
				{
					result = null;
				}
			}
			return result;
		}

		// Token: 0x0600099B RID: 2459 RVA: 0x00038588 File Offset: 0x00036788
		private byte[] PackCommand(byte[] data)
		{
			bool flag = this.isBinary;
			byte[] result;
			if (flag)
			{
				byte[] array = new byte[11 + data.Length];
				SoftBasic.HexStringToBytes("D0 00 00 FF FF 03 00 00 00 00 00").CopyTo(array, 0);
				bool flag2 = data.Length != 0;
				if (flag2)
				{
					data.CopyTo(array, 11);
				}
				BitConverter.GetBytes((short)(data.Length + 2)).CopyTo(array, 7);
				result = array;
			}
			else
			{
				byte[] array2 = new byte[22 + data.Length];
				Encoding.ASCII.GetBytes("D00000FF03FF0000000000").CopyTo(array2, 0);
				bool flag3 = data.Length != 0;
				if (flag3)
				{
					data.CopyTo(array2, 22);
				}
				Encoding.ASCII.GetBytes((data.Length + 4).ToString("X4")).CopyTo(array2, 14);
				result = array2;
			}
			return result;
		}

		// Token: 0x0600099C RID: 2460 RVA: 0x00038654 File Offset: 0x00036854
		private byte[] ReadByCommand(byte[] command)
		{
			ushort num = base.ByteTransform.TransUInt16(command, 8);
			int num2 = (int)command[6] * 65536 + (int)command[5] * 256 + (int)command[4];
			bool flag = command[2] == 1;
			byte[] result;
			if (flag)
			{
				bool flag2 = command[7] == MelsecMcDataType.M.DataCode;
				if (flag2)
				{
					result = MelsecHelper.TransBoolArrayToByteData(this.mBuffer.GetBytes(num2, (int)num));
				}
				else
				{
					bool flag3 = command[7] == MelsecMcDataType.X.DataCode;
					if (flag3)
					{
						result = MelsecHelper.TransBoolArrayToByteData(this.xBuffer.GetBytes(num2, (int)num));
					}
					else
					{
						bool flag4 = command[7] == MelsecMcDataType.Y.DataCode;
						if (!flag4)
						{
							throw new Exception(StringResources.Language.NotSupportedDataType);
						}
						result = MelsecHelper.TransBoolArrayToByteData(this.yBuffer.GetBytes(num2, (int)num));
					}
				}
			}
			else
			{
				bool flag5 = command[7] == MelsecMcDataType.M.DataCode;
				if (flag5)
				{
					result = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.mBuffer.GetBytes(num2, (int)(num * 16)), (byte m) => m > 0)).ToByteArray();
				}
				else
				{
					bool flag6 = command[7] == MelsecMcDataType.X.DataCode;
					if (flag6)
					{
						result = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.xBuffer.GetBytes(num2, (int)(num * 16)), (byte m) => m > 0)).ToByteArray();
					}
					else
					{
						bool flag7 = command[7] == MelsecMcDataType.Y.DataCode;
						if (flag7)
						{
							result = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.yBuffer.GetBytes(num2, (int)(num * 16)), (byte m) => m > 0)).ToByteArray();
						}
						else
						{
							bool flag8 = command[7] == MelsecMcDataType.D.DataCode;
							if (flag8)
							{
								result = this.dBuffer.GetBytes(num2 * 2, (int)(num * 2));
							}
							else
							{
								bool flag9 = command[7] == MelsecMcDataType.W.DataCode;
								if (!flag9)
								{
									throw new Exception(StringResources.Language.NotSupportedDataType);
								}
								result = this.wBuffer.GetBytes(num2 * 2, (int)(num * 2));
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x0600099D RID: 2461 RVA: 0x000388A0 File Offset: 0x00036AA0
		private byte[] ReadAsciiByCommand(byte[] command)
		{
			ushort num = Convert.ToUInt16(Encoding.ASCII.GetString(command, 16, 4), 16);
			string @string = Encoding.ASCII.GetString(command, 8, 2);
			bool flag = @string == MelsecMcDataType.X.AsciiCode || @string == MelsecMcDataType.Y.AsciiCode || @string == MelsecMcDataType.W.AsciiCode;
			int num2;
			if (flag)
			{
				num2 = (int)Convert.ToUInt16(Encoding.ASCII.GetString(command, 10, 6), 16);
			}
			else
			{
				num2 = (int)Convert.ToUInt16(Encoding.ASCII.GetString(command, 10, 6));
			}
			bool flag2 = command[7] == 49;
			byte[] result;
			if (flag2)
			{
				bool flag3 = @string == MelsecMcDataType.M.AsciiCode;
				if (flag3)
				{
					result = Enumerable.ToArray<byte>(Enumerable.Select<byte, byte>(this.mBuffer.GetBytes(num2, (int)num), (byte m) => (m != 0) ? 49 : 48));
				}
				else
				{
					bool flag4 = @string == MelsecMcDataType.X.AsciiCode;
					if (flag4)
					{
						result = Enumerable.ToArray<byte>(Enumerable.Select<byte, byte>(this.xBuffer.GetBytes(num2, (int)num), (byte m) => (m != 0) ? 49 : 48));
					}
					else
					{
						bool flag5 = @string == MelsecMcDataType.Y.AsciiCode;
						if (!flag5)
						{
							throw new Exception(StringResources.Language.NotSupportedDataType);
						}
						result = Enumerable.ToArray<byte>(Enumerable.Select<byte, byte>(this.yBuffer.GetBytes(num2, (int)num), (byte m) => (m != 0) ? 49 : 48));
					}
				}
			}
			else
			{
				bool flag6 = @string == MelsecMcDataType.M.AsciiCode;
				if (flag6)
				{
					bool[] array = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.mBuffer.GetBytes(num2, (int)(num * 16)), (byte m) => m > 0));
					result = MelsecHelper.TransByteArrayToAsciiByteArray(SoftBasic.BoolArrayToByte(array));
				}
				else
				{
					bool flag7 = @string == MelsecMcDataType.X.AsciiCode;
					if (flag7)
					{
						bool[] array2 = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.xBuffer.GetBytes(num2, (int)(num * 16)), (byte m) => m > 0));
						result = MelsecHelper.TransByteArrayToAsciiByteArray(SoftBasic.BoolArrayToByte(array2));
					}
					else
					{
						bool flag8 = @string == MelsecMcDataType.Y.AsciiCode;
						if (flag8)
						{
							bool[] array3 = Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(this.yBuffer.GetBytes(num2, (int)(num * 16)), (byte m) => m > 0));
							result = MelsecHelper.TransByteArrayToAsciiByteArray(SoftBasic.BoolArrayToByte(array3));
						}
						else
						{
							bool flag9 = @string == MelsecMcDataType.D.AsciiCode;
							if (flag9)
							{
								result = MelsecHelper.TransByteArrayToAsciiByteArray(this.dBuffer.GetBytes(num2 * 2, (int)(num * 2)));
							}
							else
							{
								bool flag10 = @string == MelsecMcDataType.W.AsciiCode;
								if (!flag10)
								{
									throw new Exception(StringResources.Language.NotSupportedDataType);
								}
								result = MelsecHelper.TransByteArrayToAsciiByteArray(this.wBuffer.GetBytes(num2 * 2, (int)(num * 2)));
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x0600099E RID: 2462 RVA: 0x00038BF4 File Offset: 0x00036DF4
		private byte[] WriteByMessage(byte[] command)
		{
			ushort num = base.ByteTransform.TransUInt16(command, 8);
			int num2 = (int)command[6] * 65536 + (int)command[5] * 256 + (int)command[4];
			bool flag = command[2] == 1;
			byte[] result;
			if (flag)
			{
				byte[] content = MelsecMcNet.ExtractActualData(command.RemoveBegin(10), true).Content;
				bool flag2 = command[7] == MelsecMcDataType.M.DataCode;
				if (flag2)
				{
					this.mBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Take<byte>(content, (int)num)), num2);
				}
				else
				{
					bool flag3 = command[7] == MelsecMcDataType.X.DataCode;
					if (flag3)
					{
						this.xBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Take<byte>(content, (int)num)), num2);
					}
					else
					{
						bool flag4 = command[7] == MelsecMcDataType.Y.DataCode;
						if (!flag4)
						{
							throw new Exception(StringResources.Language.NotSupportedDataType);
						}
						this.yBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Take<byte>(content, (int)num)), num2);
					}
				}
				result = new byte[0];
			}
			else
			{
				bool flag5 = command[7] == MelsecMcDataType.M.DataCode;
				if (flag5)
				{
					byte[] data = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(SoftBasic.ArrayRemoveBegin<byte>(command, 10)), (bool m) => m ? 1 : 0));
					this.mBuffer.SetBytes(data, num2);
					result = new byte[0];
				}
				else
				{
					bool flag6 = command[7] == MelsecMcDataType.X.DataCode;
					if (flag6)
					{
						byte[] data2 = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(SoftBasic.ArrayRemoveBegin<byte>(command, 10)), (bool m) => m ? 1 : 0));
						this.xBuffer.SetBytes(data2, num2);
						result = new byte[0];
					}
					else
					{
						bool flag7 = command[7] == MelsecMcDataType.Y.DataCode;
						if (flag7)
						{
							byte[] data3 = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(SoftBasic.ArrayRemoveBegin<byte>(command, 10)), (bool m) => m ? 1 : 0));
							this.yBuffer.SetBytes(data3, num2);
							result = new byte[0];
						}
						else
						{
							bool flag8 = command[7] == MelsecMcDataType.D.DataCode;
							if (flag8)
							{
								this.dBuffer.SetBytes(SoftBasic.ArrayRemoveBegin<byte>(command, 10), num2 * 2);
								result = new byte[0];
							}
							else
							{
								bool flag9 = command[7] == MelsecMcDataType.W.DataCode;
								if (!flag9)
								{
									throw new Exception(StringResources.Language.NotSupportedDataType);
								}
								this.wBuffer.SetBytes(SoftBasic.ArrayRemoveBegin<byte>(command, 10), num2 * 2);
								result = new byte[0];
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x0600099F RID: 2463 RVA: 0x00038EB4 File Offset: 0x000370B4
		private byte[] WriteAsciiByMessage(byte[] command)
		{
			ushort num = Convert.ToUInt16(Encoding.ASCII.GetString(command, 16, 4), 16);
			string @string = Encoding.ASCII.GetString(command, 8, 2);
			bool flag = @string == MelsecMcDataType.X.AsciiCode || @string == MelsecMcDataType.Y.AsciiCode || @string == MelsecMcDataType.W.AsciiCode;
			int num2;
			if (flag)
			{
				num2 = (int)Convert.ToUInt16(Encoding.ASCII.GetString(command, 10, 6), 16);
			}
			else
			{
				num2 = (int)Convert.ToUInt16(Encoding.ASCII.GetString(command, 10, 6));
			}
			bool flag2 = command[7] == 49;
			byte[] result;
			if (flag2)
			{
				byte[] array = Enumerable.ToArray<byte>(Enumerable.Select<byte, byte>(command.RemoveBegin(20), (byte m) => (m == 49) ? 1 : 0));
				bool flag3 = @string == MelsecMcDataType.M.AsciiCode;
				if (flag3)
				{
					this.mBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Take<byte>(array, (int)num)), num2);
				}
				else
				{
					bool flag4 = @string == MelsecMcDataType.X.AsciiCode;
					if (flag4)
					{
						this.xBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Take<byte>(array, (int)num)), num2);
					}
					else
					{
						bool flag5 = @string == MelsecMcDataType.Y.AsciiCode;
						if (!flag5)
						{
							throw new Exception(StringResources.Language.NotSupportedDataType);
						}
						this.yBuffer.SetBytes(Enumerable.ToArray<byte>(Enumerable.Take<byte>(array, (int)num)), num2);
					}
				}
				result = new byte[0];
			}
			else
			{
				bool flag6 = @string == MelsecMcDataType.M.AsciiCode;
				if (flag6)
				{
					byte[] data = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(MelsecHelper.TransAsciiByteArrayToByteArray(command.RemoveBegin(20))), (bool m) => m ? 1 : 0));
					this.mBuffer.SetBytes(data, num2);
					result = new byte[0];
				}
				else
				{
					bool flag7 = @string == MelsecMcDataType.X.AsciiCode;
					if (flag7)
					{
						byte[] data2 = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(MelsecHelper.TransAsciiByteArrayToByteArray(command.RemoveBegin(20))), (bool m) => m ? 1 : 0));
						this.xBuffer.SetBytes(data2, num2);
						result = new byte[0];
					}
					else
					{
						bool flag8 = @string == MelsecMcDataType.Y.AsciiCode;
						if (flag8)
						{
							byte[] data3 = Enumerable.ToArray<byte>(Enumerable.Select<bool, byte>(SoftBasic.ByteToBoolArray(MelsecHelper.TransAsciiByteArrayToByteArray(command.RemoveBegin(20))), (bool m) => m ? 1 : 0));
							this.yBuffer.SetBytes(data3, num2);
							result = new byte[0];
						}
						else
						{
							bool flag9 = @string == MelsecMcDataType.D.AsciiCode;
							if (flag9)
							{
								this.dBuffer.SetBytes(MelsecHelper.TransAsciiByteArrayToByteArray(command.RemoveBegin(20)), num2 * 2);
								result = new byte[0];
							}
							else
							{
								bool flag10 = @string == MelsecMcDataType.W.AsciiCode;
								if (!flag10)
								{
									throw new Exception(StringResources.Language.NotSupportedDataType);
								}
								this.wBuffer.SetBytes(MelsecHelper.TransAsciiByteArrayToByteArray(command.RemoveBegin(20)), num2 * 2);
								result = new byte[0];
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060009A0 RID: 2464 RVA: 0x00039224 File Offset: 0x00037424
		protected override void LoadFromBytes(byte[] content)
		{
			bool flag = content.Length < 458752;
			if (flag)
			{
				throw new Exception("File is not correct");
			}
			this.mBuffer.SetBytes(content, 0, 0, 65536);
			this.xBuffer.SetBytes(content, 65536, 0, 65536);
			this.yBuffer.SetBytes(content, 131072, 0, 65536);
			this.dBuffer.SetBytes(content, 196608, 0, 131072);
			this.wBuffer.SetBytes(content, 327680, 0, 131072);
		}

		/// <inheritdoc />
		// Token: 0x060009A1 RID: 2465 RVA: 0x000392C0 File Offset: 0x000374C0
		[HslMqttApi]
		protected override byte[] SaveToBytes()
		{
			byte[] array = new byte[458752];
			Array.Copy(this.mBuffer.GetBytes(), 0, array, 0, 65536);
			Array.Copy(this.xBuffer.GetBytes(), 0, array, 65536, 65536);
			Array.Copy(this.yBuffer.GetBytes(), 0, array, 131072, 65536);
			Array.Copy(this.dBuffer.GetBytes(), 0, array, 196608, 131072);
			Array.Copy(this.wBuffer.GetBytes(), 0, array, 327680, 131072);
			return array;
		}

		/// <summary>
		/// 释放当前的对象
		/// </summary>
		/// <param name="disposing">是否托管对象</param>
		// Token: 0x060009A2 RID: 2466 RVA: 0x0003936C File Offset: 0x0003756C
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				SoftBuffer softBuffer = this.xBuffer;
				if (softBuffer != null)
				{
					softBuffer.Dispose();
				}
				SoftBuffer softBuffer2 = this.yBuffer;
				if (softBuffer2 != null)
				{
					softBuffer2.Dispose();
				}
				SoftBuffer softBuffer3 = this.mBuffer;
				if (softBuffer3 != null)
				{
					softBuffer3.Dispose();
				}
				SoftBuffer softBuffer4 = this.dBuffer;
				if (softBuffer4 != null)
				{
					softBuffer4.Dispose();
				}
				SoftBuffer softBuffer5 = this.wBuffer;
				if (softBuffer5 != null)
				{
					softBuffer5.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		/// <summary>
		/// 获取或设置当前的通信格式是否是二进制<br />
		/// Get or set whether the current communication format is binary
		/// </summary>
		// Token: 0x17000199 RID: 409
		// (get) Token: 0x060009A3 RID: 2467 RVA: 0x000393E3 File Offset: 0x000375E3
		// (set) Token: 0x060009A4 RID: 2468 RVA: 0x000393EB File Offset: 0x000375EB
		public bool IsBinary
		{
			get
			{
				return this.isBinary;
			}
			set
			{
				this.isBinary = value;
			}
		}

		/// <inheritdoc />
		// Token: 0x060009A5 RID: 2469 RVA: 0x000393F4 File Offset: 0x000375F4
		public override string ToString()
		{
			return string.Format("MelsecMcServer[{0}]", base.Port);
		}

		// Token: 0x04000295 RID: 661
		private SoftBuffer xBuffer;

		// Token: 0x04000296 RID: 662
		private SoftBuffer yBuffer;

		// Token: 0x04000297 RID: 663
		private SoftBuffer mBuffer;

		// Token: 0x04000298 RID: 664
		private SoftBuffer dBuffer;

		// Token: 0x04000299 RID: 665
		private SoftBuffer wBuffer;

		// Token: 0x0400029A RID: 666
		private const int DataPoolLength = 65536;

		// Token: 0x0400029B RID: 667
		private bool isBinary = true;
	}
}
